package com.wyf.controller;

import com.wyf.po.Account;
import com.wyf.service.AccountService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Controller
public class AccountController {

    @Resource
    private AccountService accountService;

    /**
     * 查询用户
     * @param accountId
     * @return
     */
    @GetMapping("/account/{accountId}")
    @ResponseBody
    public Account queryAccount(@PathVariable Integer accountId) {
        return accountService.queryAccount(accountId);
    }

    /**
     * 删除用户
     * @param accountId
     * @return
     */
    @DeleteMapping("/account/{accountId}")
    // @DeleteMapping("/account/{accountId}/{name}")
    // public Map<String, Object> deleteAccount(@PathVariable Integer accountId, @PathVariable String name) {
    @ResponseBody
    public Map<String, Object> deleteAccount(@PathVariable Integer accountId) {
        Map<String, Object> map = new HashMap<>();
        int row = accountService.deleteAccount(accountId);
        if (row > 0) {
            map.put("code", 200);
            map.put("message", "delete success");
        } else {
            map.put("code", 500);
            map.put("message", "delete failed");
        }
        return map;
    }

    /**
     * 添加用户
     * @param account
     * @return
     */
    @PostMapping("/account")
    @ResponseBody
    public Map<String, Object> addAccount(@RequestBody Account account) {
        Map<String, Object> map = new HashMap<>();
        int row = accountService.addAccount(account);
        if (row > 0) {
            map.put("code", 200);
            map.put("message", "add success");
        } else {
            map.put("code", 500);
            map.put("message", "add failed");
        }
        return map;
    }

    /**
     * 更新用户
     * @param account
     * @return
     */
    @PutMapping("/account")
    @ResponseBody
    public Map<String, Object> updateAccount(@RequestBody Account account) {
        Map<String, Object> map = new HashMap<>();
        int row = accountService.updateAccount(account);
        if (row > 0) {
            map.put("code", 200);
            map.put("message", "udpate success");
        } else {
            map.put("code", 500);
            map.put("message", "udpate failed");
        }
        return map;
    }
}
